package com.nineclock.document.controller;


import com.nineclock.common.entity.Result;
import com.nineclock.doc.DocControllerApi;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.doc.DocControllerApi;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFolderDto;
import com.nineclock.document.dto.DocumentDto;
import com.nineclock.document.service.DocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class DocController implements DocControllerApi {
    @Autowired
    private DocService docService;
    @Override
    @GetMapping("/history/{fileId}")
    public Result<List<DocFileHistoryDto>> queryDocFileHistory(@PathVariable Integer fileId) {
        List<DocFileHistoryDto> dtos = docService.queryDocFileHistory(fileId);

        return Result.success(dtos);
    }


    @Override
    @PostMapping("/insertFile")
    public Result insertFile(@RequestBody DocFileDto docFileDto) {

        docService.insertFile(docFileDto);

        return Result.success();
    }

    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto docFolderDto) {
        docService.insertFolder(docFolderDto);
        return Result.success();
    }

    @Override
    @RequestMapping("/pagingFile")
    public Result<PageResult<DocFileDto>> queryDocByPage(@RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "page", defaultValue = "1") Long page,
                                                         @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize) {
        PageResult<DocFileDto> pageResult = docService.queryDocByPage(keyword, page, pageSize);
        return Result.success(pageResult);
    }

    @Override
    @PutMapping("/{fileId}/{status}")
    public Result<DocFileDto> UpdateStatus(@PathVariable Long fileId, @PathVariable Short status) {

        docService.UpdateStatus(fileId, status);

        return Result.success();

    }

    @Override
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> queryDocument(Long parentFoldId, String keyword) {

        DocumentDto documentDto = docService.queryDocument(parentFoldId, keyword);

        return Result.success(documentDto);

    }
}
